#!/usr/bin/python
 # -*- coding: utf8 -*-
import os,sys,codecs,re

def createDir(dir):
    if not os.path.exists(dir):
        os.makedirs(dir)


def main(argv):
    createDir("out") # creates output dir
    for d in os.listdir("./"):
        if os.path.isdir(d) and d!="out":
            for f in os.listdir(d):
                if os.path.isfile(d+"/"+f) and re.match(".*.JPG",f)  : # if it is a jpeg
                    filePath="./"+d+"/"+f
                    outPath="./out/"+d+"/"+f
                    createDir("out/"+d)
                    print("processing file: "+filePath)
                    os.system("convert -resize 50% -quality 80% "+filePath+" "+outPath) # convert the pic to out/oldDir
            print("")
            outArch="./out/"+d+".7z"
            inDir="./out/"+d+"/"
            print("7zip dir:"+inDir)
            os.system("7z a -t7z -mx9 "+outArch+" "+inDir) #7zip the created dir
            print("next dir")

if __name__ == "__main__":
    main(sys.argv[1:])
    
